package com.imdroid.lite.activities;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.imdroid.lite.IMDroid;
import com.imdroid.lite.R;
import com.imdroid.lite.model.User;
import com.imdroid.lite.process.ProcessFactory;
import com.imdroid.lite.process.UserProcess;

/**
 * The activity of login with users
 * 
 */
public class HomeLoginActivity extends BaseActivity {
	/**
	 * The user name
	 */
	private String userName;
	/**
	 * The password
	 */
	private String passWord;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	public void onCreate(Bundle bdl) {
		super.onCreate(bdl);
		// 如果用户已注册，则进入主页面
		User user = IMDroid.getUser();
		if (user != null) {
			startActivity(new Intent(this, MainActivity.class));
			finish();
			return;
		}

		setContentView(R.layout.home_login);
		// allowBack = false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.imdroid.lite.activities.BaseActivity#onKeyDown(int,
	 * android.view.KeyEvent)
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			System.exit(0);
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * The action of login
	 * 
	 * @param view
	 *            the button of login
	 */
	public void login(View view) {
		userName = ((EditText) this.findViewById(R.id.home_login_username))
				.getText().toString();
		passWord = ((EditText) this.findViewById(R.id.home_login_pwd))
				.getText().toString();
		// 检查输入是否完整
		if (userName == null || userName.length() <= 0) {
			Toast.makeText(this, this.getString(R.string.home_error1),
					Toast.LENGTH_SHORT).show();
			return;
		}
		if (passWord == null || passWord.length() <= 0) {
			Toast.makeText(this, this.getString(R.string.home_error2),
					Toast.LENGTH_SHORT).show();
			return;
		}
		// 用户登录
		new InProgressBarTask(this, R.string.home_wait_title,
				R.string.home_wait_reg).execute(new Object());
	}

	/**
	 * The action of register
	 * 
	 * @param view
	 *            the button of register
	 */
	public void register(View view) {
		Intent in = new Intent(this, RegisterMain.class);
//		userName = ((EditText) this.findViewById(R.id.home_login_username))
//				.getText().toString();
//		in.putExtra("user", userName);
		startActivity(in);
		finish();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.imdroid.lite.activities.BaseActivity#actionInProgressBar(java.lang
	 * .Object[])
	 */
	protected void actionInProgressBar(Object... params) throws Exception {
		// 检查用户名、密码是否正确
		UserProcess process = (UserProcess) ProcessFactory
				.getProcess(UserProcess.class);

		User currentUser = process.getUser(userName, passWord);
		// 在本机注册用户
		IMDroid.registerUser(currentUser);
		Log.d("---",currentUser.getUser_id()+"");
		// 流转到注册页面
		startActivity(new Intent(HomeLoginActivity.this, MainActivity.class));
		finish();
	}
}
